package cn.stylefeng.guns.modular.transport.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.transport.entity.MapDisptchMongo;
import cn.stylefeng.guns.modular.transport.entity.MapPoints;
import cn.stylefeng.guns.modular.transport.entity.Trace;
import cn.stylefeng.guns.modular.transport.mapper.TraceMapper;
import cn.stylefeng.guns.modular.transport.model.params.TraceParam;
import cn.stylefeng.guns.modular.transport.model.result.TraceResult;
import  cn.stylefeng.guns.modular.transport.service.TraceService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 高德轨迹信息表 服务实现类
 * </p>
 *
 * @author like
 * @since 2024-09-11
 */
@Service
public class TraceServiceImpl extends ServiceImpl<TraceMapper, Trace> implements TraceService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void add(TraceParam param){
        Trace entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TraceParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TraceParam param){
        Trace oldEntity = getOldEntity(param);
        Trace newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TraceResult findBySpec(TraceParam param){
        return null;
    }

    @Override
    public List<TraceResult> findListBySpec(TraceParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TraceParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Async("taskExecutor")
    public ResponseData uploadTracePointLocal(MapDisptchMongo mapDisptch) {


        try {
            MapDisptchMongo oldMapDisptch = mongoTemplate.findById(mapDisptch.getDispatchNumber(), MapDisptchMongo.class);
            MapDisptchMongo entity = new MapDisptchMongo();
            ToolUtil.copyProperties(mapDisptch, entity);
            List<MapPoints> points = entity.getPoints();
            MapPoints lastPoints = entity.getLastPoints();
            entity.setPoints(points);
            entity.setLastPoints(lastPoints);
            if (oldMapDisptch == null){
                mongoTemplate.save(entity, "map_disptch");
            }else {
                List<MapPoints> oldpoints = oldMapDisptch.getPoints();
                for (MapPoints point : points) {
                    oldpoints.add(point);
                }
                oldMapDisptch.setLastPoints(lastPoints);
                oldMapDisptch.setPoints(oldpoints);
                mongoTemplate.save(oldMapDisptch, "map_disptch");
            }
        } catch (Exception e) {
            return ResponseData.error("上传轨迹点失败：" + e.getMessage());
        }
        return ResponseData.success("上传轨迹点成功！");
    }

    private Serializable getKey(TraceParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Trace getOldEntity(TraceParam param) {
        return this.getById(getKey(param));
    }

    private Trace getEntity(TraceParam param) {
        Trace entity = new Trace();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
